;
(function (window, angular) {
	'use strict';
	angular.module("Controller.BasicData.MaterielClass", [])
		.controller("MaterielClassCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, UserContextService) {
			$scope.activeFlag = [true, false];
			$scope.submitted = false;
			$scope.dataList = [];
			$scope.detailInfo = {};
			$scope.tabFlag = {};
			var tabFlag = {
				0: "新增",
				1: "编辑"
			};
			$scope.root = {
				id: "0",
				isChecked: false,
				children: []
			};
			var checkedDepartList = [];
			var editingDepart = {};
			$scope.currentDepart = {};

			var btnPermissionCode = {
				materialClassAdd: window.ActionCode.materialClassAdd,
				materialClassDel: window.ActionCode.materialClassDel,
				materialClassEdit: window.ActionCode.materialClassEdit
			};

			$scope.btnPermission = {
				materialClassAdd: true,
				materialClassDel: true,
				materialClassEdit: true
			};

			function setBtnPermission() {
				for (var key in btnPermissionCode) {
					$scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
				}
			}

			$scope.changeTab = function (index) {
				$scope.activeFlag = [false, false];
				$scope.activeFlag[index] = true;
				$scope.submitted = false;
				switch (index) {
					case 0:
						initData();
						break;
					case 1:
						break;
					default:
						break;
				}
			};
			function initData() {
				$scope.tabFlag = {};
				$scope.detailInfo = {};
			}
			$scope.getChildData = function (parent) {
				if (parent.children.length > 0 && parent.id !== "0") {
					return;
				}
				var service = "device";
				var reqUrl = "/materialClass/list";
				var param = {pId: parent.id};
				ybHttp.Post(service, reqUrl, param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					angular.forEach(resp.value, function (item) {
						item.isChecked = parent.isChecked;
						item.isParent = (item.childCount > 0);
						item.children = [];
					});
					parent.children = resp.value;
				})
			};

			$scope.delData = function (parentNode, index) {
				var service = "device";
				var reqUrl = "/materialClass/remove";
				var param = {
					id:parentNode.children[index].id
				};
				ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
					ybHttp.Post(service, reqUrl, param).then(function (resp) {
						if (resp.code !== 0 && resp.value.length > 0) {
							var message = '分类' + resp.value.join(',') + '存在零件，删除失败';
							ybWidget.ShowMessage(message, MESG_TYPE.Error);
							return;
						}
						if (resp.code !== 0) {
							ybWidget.ShowMessage(resp, MESG_TYPE.Error);
							return;
						}
						ybWidget.ShowToaster('success', '操作成功');
						parentNode.children.splice(index, 1);
						if (parentNode.children.length === 0) {
							parentNode.isParent = false;
						}
					})
				}, function () {

				})
			};

			function getAllCheckDepart(node) {
				angular.forEach(node.children, function (item) {
					if (item.isChecked === true) {
						checkedDepartList.push(item.id);
					}
					if (item.children.length > 0) {
						getAllCheckDepart(item);
					}
				})
			}

			function getDetail(service, reqUrl, param) {
				return ybHttp.Get(service, reqUrl, param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return false;
					}
					return resp.value;
				});
			}

			$scope.addSubNode = function (item) {
				$scope.currentDepart = item;
				$scope.detailInfo.pId = item.id;
				$scope.detailInfo.parentNodeName = item.materialCalssName || "";
				$scope.tabFlag.type = 0;
				$scope.tabFlag.name = tabFlag[0];
				$scope.changeTab(1);
			};

			$scope.editData = function (curDepart, pareDepart) {
				editingDepart = curDepart;
				$scope.tabFlag.type = 1;
				$scope.tabFlag.name = tabFlag[1];
				$scope.detailInfo = {
					id: curDepart.id,
					parentNodeName: pareDepart.materialCalssName || "",
					materialCalssName: curDepart.materialCalssName,
					classCode: curDepart.classCode,
				};
				$scope.changeTab(1);
			};


			$scope.saveData = function (isValid) {
				if (!isValid) {
					$scope.submitted = true;
					return;
				}
				var service = "device";
				var reqUrl = "/materialClass/inser";
				if ($scope.tabFlag.type === 1) {
					reqUrl = "/materialClass/update";
				}
				var param = angular.copy($scope.detailInfo);
				console.log(param);
				ybHttp.Post(service, reqUrl, param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					if ($scope.tabFlag.type === 0) {
						var newDepart = {
							classCode: $scope.detailInfo.classCode,
							materialCalssName: $scope.detailInfo.materialCalssName,
							id: resp.value,
							childCount: 0,
							pId: $scope.currentDepart.id,
							children: []
						};
						$scope.currentDepart.isParent = true;
						$scope.currentDepart.isExpand = true;
						$scope.currentDepart.children.push(newDepart);
					} else {
						editingDepart.materialCalssName = $scope.detailInfo.materialCalssName;
						editingDepart.classCode = $scope.detailInfo.classCode;
					}
					ybWidget.ShowToaster('success', '操作成功');
					$scope.changeTab(0);
					$scope.getChildData($scope.root);
				});
			};

			(function () {
				$scope.getChildData($scope.root);
				UserContextService.SetBtnPermission(setBtnPermission);
			})();
		}])
	;
})(window, window.angular);